import 'package:flutter/material.dart';
import 'package:flutter_module/system/log_util.dart';
import 'package:flutter_module/util/event_bus.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:provider/src/provider.dart';

import 'map_bottom_drawer.dart';

class MapNewDataButton extends StatefulWidget {
  const MapNewDataButton({Key? key}) : super(key: key);

  @override
  _MapNewDataButtonState createState() => _MapNewDataButtonState();
}

class _MapNewDataButtonState extends State<MapNewDataButton>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _rotationAnimation;
  late Animation<double> _scaleAnimation;

  @override
  Widget build(BuildContext context) {
    LogUtil.instance.printf("jingo 新增按钮 build");

    /// 重要
    final DrawerManagerViewModel read = context.read<DrawerManagerViewModel>();
    return RotationTransition(
      turns: _rotationAnimation,
      child: ScaleTransition(
        child: FloatingActionButton(
          onPressed: () {
            read.getDataLayerList();
            _animationController.forward().then((value) =>
                EventBusUtils.getInstance().fire(ChangeBottomDrawerStatus(
                    DragStatus.stateHalfExpanded)));
          },
          child: const Icon(Icons.add),
        ),
        scale: _scaleAnimation,
      ), 
    );
  }

  @override
  void initState() {
    LogUtil.instance.printf("jingo 新增按钮 初始化");
    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 300));
    _rotationAnimation = Tween(begin: .0, end: 0.25).animate(CurvedAnimation(
        parent: _animationController, curve: const Interval(0.0, 0.5)));
    _scaleAnimation = Tween(begin: 1.0, end: 0.0).animate(CurvedAnimation(
        parent: _animationController, curve: const Interval(0.5, 1.0)));
    super.initState();
    EventBusUtils.getInstance().on<BottomDrawerStatusChange>().listen((event) {
      if (event.status == DragStatus.stateClose) {
        _animationController.reverse();
      }
    });
    //增加按钮联动事件
    EventBusUtils.getInstance().on<ChangeBottomDrawerStatus>().listen((event) {
      if (event.status == DragStatus.stateHalfExpanded) {
        _animationController.forward();
      }
    });
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }
}
